package com.wenyu.wenyuyuedu.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.circle.Creation_Circle_Activity;
import com.wenyu.wenyuyuedu.adapter.CircleTabAdapter;
import com.wenyu.wenyuyuedu.fragment.circlefragment.Find_Fragment;
import com.wenyu.wenyuyuedu.fragment.circlefragment.Personage_Circle_Fragment;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 圈子
 */
public class CircleFragment extends Fragment implements View.OnClickListener {

    @InjectView(R.id.tv_circle_creation)
    TextView tvCircleCreation;
    private View circleView;
    private TabLayout tab_FindFragment_title;    //定义TabLayout
    private ViewPager vp_FindFragment_pager;     //定义viewPager
    private FragmentPagerAdapter fAdapter;       //定义adapter
    private List<Fragment> list_fragment;        //定义要装fragment的集合
    private List<String> list_title;             //tab名称列表

    private Personage_Circle_Fragment personage_circle_fragment;      //我的圈子的Ftagment
    private Find_Fragment find_fragment;    //发现fragment
    private Intent intent;

    public CircleFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        circleView = inflater.inflate(R.layout.fragment_circle, container, false);
        StatusBarUtil.setStatusBar(getActivity(), R.color.white);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        ButterKnife.inject(this, circleView);
        initControls(circleView);
        initView();
        return circleView;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    private void initView() {
        intent = new Intent();
        tvCircleCreation.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //创建圈子
            case R.id.tv_circle_creation:
                intent.setClass(getActivity(), Creation_Circle_Activity.class);
                startActivity(intent);
                break;
        }
    }

    /*
    * 初始化tablayout和viewpager相关的控件
    * */
    private void initControls(View view) {
        tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tab_FindFragment_title);
        vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp_FindFragment_pager);
        //初始化两个fragment
        personage_circle_fragment = new Personage_Circle_Fragment();
        find_fragment = new Find_Fragment();
        //将fragment存放到集合当中
        list_fragment = new ArrayList<>();
        list_fragment.add(find_fragment);
        list_fragment.add(personage_circle_fragment);
        //定义存放名字的集合
        list_title = new ArrayList<>();
        list_title.add("发现");
        list_title.add("我的");
        //设置TabLayout的模式
        tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));
        tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
        fAdapter = new CircleTabAdapter(getActivity().getSupportFragmentManager(), list_fragment, list_title);
        //viewpager加载adapter
        vp_FindFragment_pager.setAdapter(fAdapter);
        //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
        //TabLayout加载viewpager
        tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
        //tab_FindFragment_title.set
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


}
